<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column
        prop="goodsName"
        label="商品名称"
        width="180"
      ></el-table-column>
      <el-table-column prop="price" label="价格" width="80"></el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <img width="50px" height="50x" :src="scope.row.img" alt="" />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="onCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'myTable',
  data() {
    return {
      tableData: [],
      total: 0,
      params: {
        page: 1,
        size: 5,
      },
    }
  },
  created() {
    this.getData()
  },
  methods: {
    onCurrentChange(page) {
      console.log('page', page)
      this.params.page = page
      // 重新调用函数渲染就可以
      this.getData()
    },
    async getData() {
      const res = await axios.get('/api/list', { params: this.params })
      console.log('data:', res.data.data.list)
      this.tableData = res.data.data.list
      this.total = res.data.data.total
    },
    handleEdit(index, row) {
      console.log('编辑', index, row)
    },
    handleDelete(index, row) {
      console.log('删除', index, row)
      this.tableData.splice(index, 1)
    },
  },
}
</script>

<style></style>
